package pl.edu.pw.MidEmu.UI.Controllers;

import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JMenuItem;

import pl.edu.pw.MidEmu.Base.Controllers.BaseController;
import pl.edu.pw.MidEmu.Common.Consts;
import pl.edu.pw.MidEmu.Common.Models.CBRDModel;
import pl.edu.pw.MidEmu.Common.Models.ClassSettingsModel;
import pl.edu.pw.MidEmu.Common.Models.MidasModel;
import pl.edu.pw.MidEmu.Logic.ProcessHandler;
import pl.edu.pw.MidEmu.UI.Components.MidasComponent;
import pl.edu.pw.MidEmu.UI.Views.RunningApplicationsListView;

/**
 * Controller for managing running application lists
 * @author Mateusz Starodab
 *
 */
public class RunningApplicationsListController extends BaseController<RunningApplicationsListView> implements MouseListener, ActionListener
{
	List<ProcessHandler> applications;

	ClassSettingsModel selectedModel;

	public RunningApplicationsListController(List<ProcessHandler> applications)
	{
		this.applications = applications;
		for (ProcessHandler process : applications)
			process.subscribeForActivityChange(this);
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() instanceof JButton)
		{
			JButton button = (JButton) e.getSource();
			if (button.getName() == Consts.RunnungApplicationList_CloseButton)
			{
				getView().close();
			}
			else if (button.getName() == Consts.RunnungApplicationList_RunButton)
			{
				Point point = MouseInfo.getPointerInfo().getLocation();
				getView().showClasLoadingFrame(point);
			}
		}
		else if (e.getSource() instanceof JMenuItem)
		{
			JMenuItem item = (JMenuItem) e.getSource();
			if (item.getName() == Consts.RunnungApplicationList_ShowOutput)
			{
				if (selectedModel != null)
				{
					Point point = MouseInfo.getPointerInfo().getLocation();
					getView().showOutputWindow(selectedModel, point);
					selectedModel = null;
				}
			}
			if (item.getName() == Consts.RunnungApplicationList_ShowSettings)
			{
				if (selectedModel != null)
				{
					Point point = MouseInfo.getPointerInfo().getLocation();
					getView().showSettingsWindow(selectedModel, point);
					selectedModel = null;
				}
			}
		}
	}

	public List<ClassSettingsModel> getActiveApplicationModels()
	{
		List<ClassSettingsModel> models = new ArrayList<ClassSettingsModel>();

		MidasComponent comp = getView().getParent();
		MidasModel midasModel = comp.getController().getMidas();
		if (midasModel != null && comp.getController().isMidasRunning())
			models.add(midasModel);

		CBRDModel cbrdModel = comp.getController().getCBRD();
		if (cbrdModel != null && comp.getController().isCBRDRunning())
			models.add(cbrdModel);

		for (ProcessHandler process : applications)
			if (process.isActive())
				models.add(process.getModel());

		return models;
	}

	public List<ClassSettingsModel> getInactiveApplicationModels()
	{
		List<ClassSettingsModel> models = new ArrayList<ClassSettingsModel>();

		MidasComponent comp = getView().getParent();
		MidasModel model = comp.getController().getMidas();
		if (model != null && !comp.getController().isMidasRunning())
			models.add(model);

		CBRDModel cbrdModel = comp.getController().getCBRD();
		if (cbrdModel != null && !comp.getController().isCBRDRunning())
			models.add(cbrdModel);

		for (ProcessHandler process : applications)
			if (!process.isActive())
				models.add(process.getModel());
		return models;
	}

	@Override
	public void mouseClicked(MouseEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e)
	{
		if (e.getButton() == MouseEvent.BUTTON3)
		{
			if (e.getSource() instanceof JList)
			{
				JList list = (JList) e.getSource();
				int index = list.locationToIndex(e.getPoint());
				boolean isInside = list.getCellBounds(index, index).contains(e.getPoint());
				if (isInside)
					selectedModel = (ClassSettingsModel) list.getModel().getElementAt(index);
				else
					selectedModel = null;
				getView().showPopupMenu(e.getComponent(), e.getX(), e.getY(), isInside);
			}
		}
	}

	@Override
	public void mouseReleased(MouseEvent e)
	{
		// TODO Auto-generated method stub
	}

	public void rebindModels()
	{
		getView().rebindModels();
	}
}
